﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProTrack.Api.Helper;
using ProTrack.Dto;
using ProTrack.IServices.Stock;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProTrack.Api.Controllers
{
    /// <summary>
    /// 库存查询控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ItemStockController : ControllerBase
    {
        public ItemStockController(IItemStockServices services, LogHelper LogHelper)
        {
            Services = services;
            this.LogHelper = LogHelper;
        }

        public IItemStockServices Services { get; }
        public LogHelper LogHelper { get; }



        /// <summary>
        /// 分页获取库存
        /// </summary>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="WarehouseId">仓库Id</param>
        /// <param name="ItemTypeId">物料类型Id</param>
        /// <param name="ItemName">商品名称</param>
        /// <returns></returns>

        [HttpGet]
        public async Task<IActionResult> Get([FromQuery] int PageIndex, [FromQuery] int PageSize, [FromQuery] string WarehouseId, [FromQuery] string ItemTypeId, [FromQuery] string ItemName)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                WarehouseId = WarehouseId ?? "";
                ItemTypeId = ItemTypeId ?? "";
                ItemName = ItemName ?? "";

                var list = await Services.GetAsync(PageIndex, PageSize, WarehouseId, ItemTypeId, ItemName);
                status.Data = list;
                status.Count = await Services.GetCountAsync(WarehouseId, ItemTypeId, ItemName);
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("库存查询-分页获取", ex, HttpContext);
            }

            return Ok(status);
        }


        /// <summary>
        /// 分页获取明细
        /// </summary>
        /// <param name="WarehouseId">仓库Id</param>
        /// <param name="OrderTypeName">订单类型名称</param>
        /// <param name="ItemId">商品Id</param>
        /// <returns></returns>
        [HttpGet("GetDetails")]
        public async Task<IActionResult> GetDetails([FromQuery] string WarehouseId, [FromQuery] string OrderTypeName, [FromQuery] string ItemId)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                OrderTypeName = OrderTypeName ?? "";
                if (string.IsNullOrEmpty(WarehouseId) ||string.IsNullOrEmpty(ItemId))
                {
                    status.Code = -1;
                    status.Msg = "参数有误！";

                    return Ok(status);
                }

                var list = await Services.GetDetailsAsync(WarehouseId, ItemId, OrderTypeName);
                status.Data = list;
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("库存查询-分页获取", ex, HttpContext);
            }

            return Ok(status);
        }











    }
}
